package com.wangs.redis;

import com.wangs.redis.utils.SerializeUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.Transaction;
import redis.clients.jedis.params.SetParams;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@SpringBootTest
@RunWith(SpringRunner.class)
public class JedisDemo {

    @Autowired
    private JedisPool jedisPool;

    private Jedis jedis;

    @Before
    public void init() {
        jedis = jedisPool.getResource();
    }

    @After
    public void after() {
        if (null != jedis) {
            jedis.close();
        }
    }

    @Test
    public void test01() {
        Jedis jedis = new Jedis("127.0.0.1", 6379);
//        jedis.auth("123456");//连接密码
        jedis.select(0);
        String ping = jedis.ping();
        System.out.println(ping);
        jedis.set("name", "wst");
        String name = jedis.get("name");
        System.out.println(name);

        if (null != jedis) {
            jedis.close();
        }
    }

    @Test
    public void test02() {

        JedisPool jedisPool = new JedisPool(new JedisPoolConfig(),
                "127.0.0.1", 6379, 10000);

        Jedis jedis = jedisPool.getResource();
//        jedis.auth("123456");//连接密码
        jedis.select(0);
        String ping = jedis.ping();
        System.out.println(ping);
        jedis.set("name", "汪涛");
        String name = jedis.get("name");
        System.out.println(name);

        if (null != jedis) {
            jedis.close();
        }
    }

    @Test
    public void test03() {
        String ping = jedis.ping();
        System.out.println(ping);
    }

    @Test
    public void testString() {
        jedis.set("name", "汪涛");
        String name = jedis.get("name");
        System.out.println(name);

        jedis.mset("name", "wangtao", "age", "23", "weight", "22");
        List<String> mget = jedis.mget("name", "age", "weight");
        mget.forEach(System.out::println);

        jedis.del("name");

        mget.forEach(System.out::println);
    }

    @Test
    public void testHash() {
        jedis.hset("user", "name", "wst");
        String hget = jedis.hget("user", "name");
        System.out.println(hget);

        Map<String, String> map = new HashMap<>();
        map.put("say", "你说的是啥？");
        map.put("log", "是这个玩意");

        jedis.hmset("map", map);
        List<String> hmget = jedis.hmget("map", "say", "log");
        hmget.forEach(System.out::println);

        jedis.hdel("map", "log");

    }

    @Test
    public void testList() {
        jedis.lpush("users", "wst", "wt", "wst", "ls");
        jedis.rpush("users", "zs", "wer");
        List<String> users = jedis.lrange("users", 0, 8);
        users.forEach(System.out::println);

        Long users1 = jedis.llen("users");
        System.out.println(users1);

        jedis.lrem("users", 1, "wst");


    }

    @Test
    public void testSet() {
        jedis.sadd("letters", "aaa", "bbb", "dddd", "dddd", "ccc");
        Set<String> letters = jedis.smembers("letters");
        letters.forEach(System.out::println);

        Long letters1 = jedis.scard("letters");
        System.out.println(letters1);

        jedis.srem("letters", "aaa");
    }


    @Test
    public void testSortedSet() {
        Map<String, Double> map = new HashMap<>();
        map.put("wst", 50d);
        map.put("wt", 60d);
        map.put("s", 64d);

        jedis.zadd("score", map);

        Set<String> score = jedis.zrange("score", 0, 3);
        score.forEach(System.out::println);

        Long score1 = jedis.zcard("score");
        System.out.println(score1);

        jedis.zrem("score", "wt");

    }

    @Test
    public void test1() {
        jedis.set("users:01:item", "wst");
        String s = jedis.get("users:01:item");
        System.out.println(s);

        Set<String> keys = jedis.keys("*");
        keys.forEach(System.out::println);

        Long aLong = jedis.dbSize();
        System.out.println(aLong);
    }

    @Test
    public void testExpire() {
        jedis.setex("name", 20, "wst");

        Long name = jedis.ttl("name");

        jedis.expire("name", 20);
        System.out.println(name);

        SetParams ex = new SetParams().nx().ex(10);
        jedis.set("code", "test", ex);
    }

    @Test
    public void SerialObject() {
        User user = new User();
        user.setId(11);
        user.setUserName("汪涛");
        user.setPassword("11111");

        byte[] bytes = SerializeUtils.serializeObject(user);
        jedis.set(SerializeUtils.serializeObject("user"), bytes);

        byte[] users = jedis.get(SerializeUtils.serializeObject("user"));
        Object o = SerializeUtils.deserializeObject(users);
        User user1 = (User) o;

        System.out.println(user1);
    }

    @Test
    public void testTx() {
        Transaction transaction = jedis.multi();
        transaction.set("name", "234");
        transaction.exec();
//        transaction.discard();
    }


}
